﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Jayrock.Json.Conversion;
using QzoneSDK;

namespace NJL.Web
{
    public partial class QQCallback : BasePageVisitor
    {
        protected string AUTH_GUID;

        protected void Page_Load(object sender, EventArgs e)
        {
            string strFromWebSiteCode = Session["FromWebSiteCode"].ToString();
            string strFromDomain = Session["FromDomain"].ToString();
            string strFromUrl = Session["FromUrl"].ToString();
            string strFromLoginType = Session["FromLoginType"].ToString();

            try
            {
                if (Request.QueryString["oauth_vericode"] != null)
                {

                    var requestTokenKey = Session["requesttokenkey"].ToString();

                    var requestTokenSecret = Session["requesttokensecret"].ToString();

                    var verifier = Request.QueryString["oauth_vericode"];

                    string key = ConfigurationManager.AppSettings["ConsumerKey"];

                    string secret = ConfigurationManager.AppSettings["ConsumerSecret"];

                    QzoneSDK.Qzone qzone = new QzoneSDK.Qzone(key, secret, requestTokenKey, requestTokenSecret, verifier);

                    //这里需要将qzone.OAuthTokenKey, qzone.OAuthTokenSecret, qzone.OpenID 存储起来用于后面的API的访问

                    //QzoneSDK.Qzone qzone2 = new QzoneSDK.Qzone(key, secret, qzone.OAuthTokenKey, qzone.OAuthTokenSecret, string.Empty, true, qzone.OpenID);
                    //QzoneSDK.Qzone qzone2 = new QzoneSDK.Qzone(key, secret, qzone.OAuthTokenKey, qzone.OAuthTokenSecret, string.Empty, false, qzone.OpenID);

                    //Session["qzonesdk"] = qzone2;
                    Session.Add("qzonesdk", qzone);

                    qzone = Session["qzonesdk"] as QzoneSDK.Qzone;

                    //var currentUser = qzone2.GetCurrentUser();
                    var currentUser = qzone.GetCurrentUser();
                    var user = (QzoneSDK.Models.BasicProfile)JsonConvert.Import(typeof(QzoneSDK.Models.BasicProfile), currentUser);

                    if (null != user)
                    {

                        //this.Nickname.Text = user.Nickname;

                        //this.Figureurl.Text = user.Figureurl;

                        Model.NJL_USER model = new BLL.NJL_USER().GetModelByQQOpenID(qzone.OpenID);
                        if (null == model)
                        {
                            model = new Model.NJL_USER();
                            model.ID = System.DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
                            model.NJL_SYS_BASE_ID = strFromWebSiteCode;
                            model.USER_NAME = user.Nickname;
                            model.QQ_OPENID = qzone.OpenID;
                            model.TYPE_CODE = "2";//直接跳过
                            model.INTEGRAL = 0;
                            model.CREATE_DATE_TIME = System.DateTime.Now;
                            new BLL.NJL_USER().Add(model);

                            //添加个人信息
                            Model.NJL_PERSON modelPerson = new Model.NJL_PERSON();
                            modelPerson.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
                            modelPerson.NJL_SYS_BASE_ID = strFromWebSiteCode;
                            modelPerson.CREATE_DATE_TIME = DateTime.Now;
                            modelPerson.NJL_USER_ID = model.ID;
                            new BLL.NJL_PERSON().Add(modelPerson);

                            Session.Clear();
                            Session.Abandon();
                            Session.Add("userid", model.ID);
                            Session.Add("username", model.USER_NAME);
                            Session.Add("usertype", model.TYPE_CODE);

                            string userType = model.TYPE_CODE;
                            string strToURL = "";
                            //如果未激活先到激活页面
                            if (userType == "0")
                            {
                                strToURL = "/RegisterTow.aspx";
                            }
                            else if (strFromUrl == "")
                            {
                                strToURL = "/";
                            }
                            else if (strFromUrl != "")
                            {
                                strToURL = strFromUrl;
                            }
                            else
                            {
                                strToURL = "/";
                            }

                            ShareSession.ShareSessionFormatter.Serialize(ShareSession.ShareSessionFormatter.FormatterType.Soap);
                            AUTH_GUID = Session["AUTH_GUID"].ToString();

                            string strURL = "http://" + strFromDomain + "/GetUserSessions.aspx?AUTH_GUID=" + AUTH_GUID + "&logintype=" + strFromLoginType + "&tourl=" + strToURL;
                            Response.Redirect(strURL, false);
                        }
                        else
                        {
                            Session.Clear();
                            Session.Abandon();
                            Session.Add("userid", model.ID);
                            Session.Add("username", model.USER_NAME);
                            Session.Add("usertype", model.TYPE_CODE);

                            string userType = model.TYPE_CODE;
                            if (model.TYPE_CODE == "0")
                            {
                                model.TYPE_CODE = "2";
                                new BLL.NJL_USER().Update(model);
                            }
                            string strToURL = "";
                            //如果未激活先到激活页面
                            if (userType == "0")
                            {
                                strToURL = "/RegisterTow.aspx";
                            }
                            else if (strFromUrl == "")
                            {
                                strToURL = "/";
                            }
                            else if (strFromUrl != "")
                            {
                                strToURL = strFromUrl;
                            }
                            else
                            {
                                strToURL = "/";
                            }

                            ShareSession.ShareSessionFormatter.Serialize(ShareSession.ShareSessionFormatter.FormatterType.Soap);
                            AUTH_GUID = Session["AUTH_GUID"].ToString();

                            string strURL = "http://" + strFromDomain + "/GetUserSessions.aspx?AUTH_GUID=" + AUTH_GUID + "&logintype=" + strFromLoginType + "&tourl=" + strToURL;
                            Response.Redirect(strURL, false);
                        }
                    }
                    else
                    {
                        //Response.Redirect("/",true);
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "message", "<script language='javascript' defer>window.parent.window.location.href = '/';window.close();</script>");
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("/LoginToQQ.aspx?websitecode=" + strFromWebSiteCode + "&domain=" + strFromDomain + "&url=" + strFromUrl);
                //Response.Write(ex.Message.ToString());
            }

        }
    }
}